#include "basewidget.h"

BaseWidget::BaseWidget(QWidget *parent) : QWidget(parent)
{
    setAttribute(Qt::WA_DeleteOnClose);//关闭时回收内存

    connect(KeyThread::Get(), &KeyThread::keyEvent, this, &BaseWidget::KeyEvent);
    setFixedSize(1024, 600);

    loading = new Loading(this);
    loading->setGeometry((1024 - 300)/2, (600-300)/2, 300, 300);
    //loading->startLoading("/gui/res/load.png", 50);

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &BaseWidget::sigLongPress);
}

BaseWidget::~BaseWidget()
{
    emit sigClose();
}

void BaseWidget::mousePressEvent(QMouseEvent *e)
{
    isPress = true;
    pressPos = e->pos();
    timer->start(1500);
}

void BaseWidget::mouseMoveEvent(QMouseEvent *e)
{
    if(isPress)
    {
        if((e->pos().x() - pressPos.x() > 10) || (e->pos().y() - pressPos.y() > 10) || \
            e->pos().x() - pressPos.x() < -10 || (e->pos().y() - pressPos.y() < -10))
        {
            if(!isMove)
            {
                isMove = true;
                timer->stop();
            }
        }
    }
}

void BaseWidget::mouseReleaseEvent(QMouseEvent *)
{
    isPress = false;
    isMove = false;
}



